package com.r2coding.ent.controller;

import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import com.r2coding.ent.annotation.SysLog;
import com.r2coding.ent.service.AlbumService;
import com.r2coding.ent.vo.req.BatchDeletePictureReqVo;
import com.r2coding.ent.vo.req.MovePictureReqVo;
import com.r2coding.ent.vo.req.SavePictureReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.r2coding.ent.service.PictureService;
import com.r2coding.common.util.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 图片
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-06-27 21:53:54
 */
@RestController
@RequestMapping("/picture")
public class PictureController {
    @Autowired
    private PictureService pictureService;

    @Autowired
    AlbumService albumService;

    /**
     * 相册添加照片
     *
     * @param req 请求
     * @return 统一响应体
     */
    @SysLog(module = Module.ENT, operationType = OperationType.INSERT, description = "添加照片")
    @PreAuthorize("hasAuthority('picture:add')")
    @PostMapping("/savePicture")
    public Result SavePicture(@RequestBody SavePictureReqVo req) {
        albumService.savePicture(req);
        return Result.ok();
    }

    /**
     * 根据相册id和照片id删除照片
     *
     * @param albumId 相册id
     * @param id 照片id
     * @return 统一响应体
     */
    @SysLog(module = Module.ENT, operationType = OperationType.DELETE, description = "删除照片")
    @PreAuthorize("hasAuthority('picture:delete')")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        pictureService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除
     *
     * @param ids 待删除的照片ids
     * @return 统一响应体
     */
    @SysLog(module = Module.ENT, operationType = OperationType.DELETE, description = "删除照片")
    @PreAuthorize("hasAuthority('picture:delete')")
    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody BatchDeletePictureReqVo req) {
        pictureService.removeByIds(req.getIds());
        return Result.ok();
    }

    /**
     * 移动照片到其他相册
     *
     * @param req 请求信息
     * @return 统一响应体
     */
    @SysLog(module = Module.ENT, operationType = OperationType.UPDATE, description = "移动照片")
    @PreAuthorize("hasAuthority('picture:move')")
    @PostMapping("/movePicture")
    public Result movePicture(@RequestBody MovePictureReqVo req) {
        pictureService.movePicture(req);
        return Result.ok();
    }

}
